package leetcode.code130;

public class Solution extends Solution130 {

	private final char X = 'X';
	private final char O = 'O';
	private final char G = 'G';
	private int x;
	private int y;

	@Override
	public void solve(char[][] board) {
		x = board.length;
		y = board[0].length;
		for (int i = 0; i < y; i++) {
			this.infected(board, 0, i);
			this.infected(board, x - 1, i);
		}
		for (int i = 0; i < x; i++) {
			this.infected(board, i, 0);
			this.infected(board, i, y - 1);
		}
		for (int i = 0; i < x; i++) {
			for (int j = 0; j < y; j++) {
				if (board[i][j] == O) {
					board[i][j] = X;
				} else if (board[i][j] == G) {
					board[i][j] = O;
				}
			}
		}
	}

	private void infected(char[][] board, int row, int col) {
		if (row >= 0 && row < x && col >= 0 && col < y && board[row][col] == O) {
			board[row][col] = G;
			this.infected(board, row + 1, col);
			this.infected(board, row - 1, col);
			this.infected(board, row, col + 1);
			this.infected(board, row, col - 1);
		}
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();

	}

}
